//
//  QYRemindMassage.m
//  ocDemo
//
//  Created by Joggy on 16/3/29.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "QYNotice.h"

static QYNotice *instance;

@implementation QYNotice

-(instancetype)init {
    self = [super initWithFrame:CGRectMake(0, -QYHeight, QYWidth, QYHeight)];
    self.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, QYWidth, QYHeight)];
    _label.font = [UIFont systemFontOfSize:12];
    _label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_label];
    return self;
}

+(QYNotice*)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[QYNotice alloc] init];
    });
    return instance;
}

+(void)showSuccess:(NSString *)title toView:(UIView *)view {
    [[self shared] remove];
    [self shared].label.textColor = [UIColor colorWithRed:0.3 green:0.69 blue:0.498 alpha:1];
    [self shared].label.text = title;
    [view addSubview:[self shared]];
    [[self shared] show];
}

+(void)showError:(NSString *)title toView:(UIView *)view {
    [[self shared] remove];
    [self shared].label.textColor = [UIColor redColor];
    [self shared].label.text = title;
    [view addSubview:[self shared]];
    [[self shared] show];
}

+(void)showWarning:(NSString *)title toView:(UIView *)view {
    [[self shared] remove];
    [self shared].label.textColor = [UIColor colorWithRed:1 green:0.498 blue:0 alpha:1];
    [self shared].label.text = title;
    [[self shared] addSubview:[self shared].label];
    [view addSubview:[self shared]];
    [[self shared] show];
}

+(void)showProcess:(NSString *)title toView:(UIView *)view {
    [[self shared] remove];
    [self shared].label.textColor = [UIColor colorWithRed:0.47 green:0.5647 blue:0.61 alpha:1];
    [self shared].label.text = title;
    [view addSubview:[self shared]];
    [[self shared] process];
}

+(void)dismissWithDelay:(double)time {
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time*NSEC_PER_SEC));
//    dispatch_after(popTime, dispatch_get_main_queue(), ^{
//        if([self shared].timer != nil) {
//            [[self shared].timer invalidate];
//        }
//        [UIView animateWithDuration:1 animations:^{
//            CGRect tem = [self shared].frame;
//            tem.origin.y = -QYHeight;
//            [self shared].frame = tem;
//        } completion:^(BOOL finished) {
//            [self shared].timer = nil;
//            [[self shared] removeFromSuperview];
//        }];
//    });
    [self dismissWithDelay:time completed:nil];
}

+(void)dismissWithDelay:(double)time completed:(void (^)())complete {
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time*NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^{
        if([self shared].timer != nil) {
            [[self shared].timer invalidate];
        }
        [UIView animateWithDuration:1 animations:^{
            CGRect tem = [self shared].frame;
            tem.origin.y = -QYHeight;
            [self shared].frame = tem;
        } completion:^(BOOL finished) {
            [self shared].timer = nil;
            [[self shared] removeFromSuperview];
//            complete();
        }];
    });
}

-(void)process {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(show) userInfo:nil repeats:true];
}

-(void)show {
    CGRect tem = self.frame;
    tem.origin.y = -QYHeight;
    self.frame = tem;
    [UIView animateWithDuration:1 animations:^{
        CGRect tem = self.frame;
        tem.origin.y = 0;
        self.frame = tem;
    }];
}

-(void)remove {
    if(_timer != nil) {
        [_timer invalidate];
        _timer = nil;
    }
    [self removeFromSuperview];
}

@end

